package cn.jxau.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import cn.jxau.bean.po.Article;
import cn.jxau.bean.po.ArticleType;
import cn.jxau.bean.po.User;
import cn.jxau.bean.vo.JsonResult;
import cn.jxau.common.util.JacksonUtils;
import cn.jxau.common.util.WebTools;

/**
 * 跟主页相关的请求处理，用户可以不用登陆
 * @author licho
 *
 */
@RequestMapping("/")
//user代表User对象，juser代表json格式数据的user
@SessionAttributes(value={"user","juser"})
@Controller
public class IndexController extends BaseController{
	private static final Logger log=Logger.getLogger(IndexController.class);
	/**
	 * 主页
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/index")
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response){
		JsonResult result = renderSuccess("响应成功");
		ModelAndView mv = new ModelAndView();
		List<ArticleType> articleTypes = articleTypeService.getAllArticleType();
		List<Article> articles = articleService.getAllArticle();
		result.addData("articles", articles);
		result.addData("articleTypes", articleTypes);
		result.addData("parentAndSon", articleTypeService.initParentAndSon());
		mv.addObject("result", result);
		return mv;
	}
	
	/**
	 * 返回json数据
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/json")
	public JsonResult getJson(){
		return renderSuccess("恭喜你成功啦！");
	}
	
	/**
	 * 直接获取静态页面
	 * @return
	 */
	@RequestMapping("/table")
	public String getHtml(){
		return "table";
	}
	/**
	 * 完成用户登录注册的功能
	 * @return
	 */
	@RequestMapping(value="/register",method={RequestMethod.POST})
	@ResponseBody
	public JsonResult register(User user,@RequestParam("file")MultipartFile file){
		JsonResult result=null;
		String fileURL=WebTools.saveFile(file);//返回的文件名
		user.setAvator(fileURL);
		boolean flag=indexService.insertUserForRegister(user);//添加用户
		if(flag)
			result=renderSuccess("注册成功");
		else
			result=renderError("注册失败");
		return result;
	}
	
	/**
	 * 该请求处理用户的登录请求
	 * @param login	用户的登录名或者邮箱
	 * @param pwd	用户名密码
	 * @param isRember	是否要记住密码，该参数默认为false
	 * @return
	 */
	@RequestMapping(value={"/login"},method={RequestMethod.POST})
	public ModelAndView login(@RequestParam("login")String login,@RequestParam("pwd")String pwd,@RequestParam(name="rember",defaultValue="false")Boolean isRember){
		ModelAndView mv = new ModelAndView();
		User user = userService.getUserByNameOrEmail(login, pwd);
		if(user!=null){
			mv.addObject("user", user);//写入user
			mv.addObject("juser", JacksonUtils.converToJson(user));//写入json对象
			mv.setViewName("redirect:/index");
		}else{
			mv.addObject("msg","登录失败!您的账号不存在或已被加入黑名单！");
			mv.setViewName("login");
		}
		return mv;
	}
	
	
	/**
	 * 该请求处理用户的ajax登录请求
	 * @param login	用户的登录名或者邮箱
	 * @param pwd	用户名密码
	 * @return
	 */
	@RequestMapping("ajaxlogin")
	@ResponseBody
	public JsonResult ajaxlogin(String login,String pwd,HttpSession session){
		JsonResult result =  null;
		System.out.println("----------");
		System.out.println(login + "-" + "pwd");
		User user = userService.getUserByNameOrEmail(login, pwd);
		if(user!=null){
			result = renderSuccess("登录成功啦！");
			result.addData("user", user);
			session.setAttribute("user", user);
			result.addData("juser", JacksonUtils.converToJson(user));
		}else{
			result = renderError("登录失败!您的账号不存在或已被加入黑名单！");
		}
		return result;
	}
	/**
	 * 注销
	 * 移除session内的值
	 */
	@RequestMapping("/logout")
	@ResponseBody
	public ModelAndView logout(SessionStatus status){
		ModelAndView mv = new ModelAndView();
		status.setComplete();
		mv.setViewName("redirect:/index");
		return mv;
	}
}
